home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12551 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  45 lines

  1. Path: quadostimpy.natinst.com!user
  2. From: rcauvin@natinst.com (Roger L. Cauvin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Dynamic Casting / RTTI
  5. Date: 20 Mar 1996 15:56:03 GMT
  6. Organization: National Instruments
  7. Message-ID: <rcauvin-2003961001170001@quadostimpy.natinst.com>
  8. References: <DoJF68.Mx8@news.uwindsor.ca>
  9. NNTP-Posting-Host: quadostimpy.natinst.com
  10.  
  11. In article <DoJF68.Mx8@news.uwindsor.ca>, saed@engn.uwindsor.ca wrote:
  12.  
  13. > Hi All,
  14. > given the following code:
  15. > class Base{};
  16. > class Deri : public Base{};
  17. > main(){  Base * pB = new Deri; };
  18. > --- How do can I obtain a 'Deri' class pointer to the same object as pB?
  19. > sort of like: Deri * pD = dynamic_cast<Deri*>(pB);
  20. > but this yields an error: 
  21. > cannot dynamic_cast `pB' (of type `class Base *') to type `class Deri *'
  22.  
  23. Two things:
  24.  
  25. 1.  A dynamic cast is not needed in the example given above.  You could
  26. use a static cast instead without any compiler complaints.
  27.  
  28. 2.  In order for a dynamic cast to work, the base class must have at least
  29. one virtual function.  The virtual table is used to store the runtime type
  30. information, and if one is not present, then no RTTI exists either.
  31.  
  32.  
  33. Roger
  34.  
  35. ---
  36.  
  37. Roger L. Cauvin
  38. rcauvin@natinst.com
  39. Software Engineer
  40. National Instruments
  41.